let express = require("express");
let http = require("http");
let request = require("request");
var router = express.Router();

var token =
  "24.157f5924581ef56d0e2accffdb637b1e.2592000.1638595648.282335-25105024";

//语音识别的接口
router.post("/record", (req, res) => {
  let { pcm, size } = req.body;
  let size1 = +size;
  console.log(req.body);
  let options = {
    host: "vop.baidu.com",
    path: "/server_api",
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
  };
  console.log(size);
  let contents = JSON.stringify({
    // url  图片类型
    format: "pcm",
    rate: 16000,
    dev_pid: 1537,
    channel: 1,
    token: token,
    cuid: "3C-95-09-35-61-9B",
    len: size1,
    speech:pcm // xxx为 base64（FILE_CONTENT）
  });
  let rawData = "";
  let req_baiduai = http.request(options, function (res_baiduai) {
    res_baiduai.setEncoding("utf8");
    res_baiduai.on("data", function (chunk) {
      rawData = `${rawData}${chunk}`;
    }),
      res_baiduai.on("end", function () {
        res.send(rawData);
        var testResult = JSON.parse(rawData);
        console.log(rawData);
        res.end();
      });
  });
  req_baiduai.write(contents);
  req_baiduai.end();
});

module.exports = router;
